// pages/accountDetail/accountDetail.js
const app = getApp();
import userApi from '../../service/userApi'
let codeConfig = app.globalData.codeConfig
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadTips: "上拉加载",
    showPupFlag: false,
    startPage: 1,
    pageSize: 15,
    loading: false,
    data: {
    },
    bizTypeStatus: {...codeConfig.bizTypeStatus},
    upAbled: true,
    istalent: 'false',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.isTalent) {
      this.setData({
        istalent: options.isTalent
      })
    }
    if (this.data.istalent == 'true') {
      this.getInitData()
    } else {
      this.setData({
        upAbled: false
      })
    }
  },
  showPup() {
    this.setData({
      showPupFlag: true
    });
  },
  closePup() {
    this.setData({
      showPupFlag: false
    });
  },
  // 获取账户明细
  getInitData() {
    var that = this;
    that.setData({
      loading: true,
      loadTips: "加载中..."
    });
    wx.showLoading({
      title: "请稍后"
    });
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    };
    userApi.getFlowing(params).then(res => {
        wx.hideLoading();
        that.setData({
          loading: false
        });
        wx.stopPullDownRefresh();
        if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
          that.setData({
            data: res.data.data
          });
          if (res.data.data.earningsList.length < that.data.pageSize) {
            that.setData({
              loadTips: "暂无更多数据",
              upAbled: false
            });
          } else if (res.data.data.earningsList.length === 0) {
            that.setData({
              loadTips: "暂无数据"
            });
          } else {
            that.setData({
              loadTips: "上拉加载"
            });
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      })
      .catch(e => {
        console.log(e);
        that.setData({
          loading: false
        });
        wx.stopPullDownRefresh();
        wx.hideLoading();
      });
  },
  goPage (e) {
    var url = e.currentTarget.dataset.url
    var type = e.currentTarget.dataset.type
    if (type === 'refresh') {
      app.globalData.refreshIndexFlag = true
    }
    if (url.indexOf('openTalent') > 0) {
      wx.navigateTo({
        url
      })
    } else {
      wx.reLaunch({
        url
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (this.data.istalent == 'false') {
      wx.stopPullDownRefresh()
    } else {
      this.setData({
        startPage: 1,
        pageSize: 15,
        upAbled: true // 是否可上拉加载
      })
      this.getInitData()
    }
  },

/**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.upAbled) {
      return false
    }
    if (that.data.loading) {
      return false
    }
    that.setData({
      loading: true,
      loadTips: '加载中...',
      startPage: that.data.startPage + 1
    })
    wx.showLoading({
      title: "请稍后"
    });
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    }
    userApi.getFlowing(params).then(res => {
      wx.hideLoading();
      that.setData({
        loading: false
      })
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        var _data = res.data.data
        var arr = that.data.data.earningsList.concat(_data.earningsList)
        that.setData({
          'data.earningsList': arr
        })
        if (res.data.data.earningsList.length < that.data.pageSize) {
          that.setData({
            loadTips: '暂无更多数据',
            startPage: that.data.startPage - 1,
            upAbled: false
          })
        } else {
          that.setData({
            loadTips: '上拉加载'
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }).catch(e => {
      console.log(e)
      that.setData({
        loading: false
      })
    })
  }
});
